"""
功能描述 ：
编写人 ：
编写日期 ：
实现逻辑 ：
    1- ......
        1.1- ......
    2- ......
        2.1- ......
"""
import smtplib,time, os
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email .header import Header

class SendEmail():
    def __init__(self):
        # 第一步：配置邮箱属性
        # 发送邮箱
        self.sender = 'mingwei0713@126.com'
        # 接收邮箱
        self.receiver = ['990844529@qq.com', "347813206@qq.com"]
        # 发送邮件主题
        t = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
        self.subject = '自动化测试结果_' + t
        # 发送邮箱服务器
        self.smtpserver = 'smtp.126.com'
        # 发送邮箱用户/密码
        self.username = 'mingwei0713@126.com'
        self.password = 'HDSWDHGNKWCRQFNN'
    def __config(self,file):
        with open(file,'rb') as f:
            mail_bady = f.read()
            self.msg = MIMEMultipart
            #附件内容
            att = MIMEText(mail_bady,'plain','utf-8')
            att["Content-Type"] = 'attachment; filename=report.html'
            self.msg.attach(att)
            #添加文本内容
            content = '自动化测试报告详情，请查收附件'
            self.msg.attach(MIMEText(content,'plain','utf-8'))
            self.msg['Subject'] = Header(self.subject, 'utf-8')
            self.msg['From'] = self.sender
            self.msg['To'] = ",".join(self.receiver)
    def send(self,file):
        self.__config(file)
        try:
            # 1--实例化smtp类
            smtp = smtplib.SMTP()
            # 2--连接stmp服务器
            smtp.connect(self.smtpserver)
            # 3--登录邮箱
            smtp.login(self.username, self.password)
            # 4--设置发件人，收件人，邮件内容
            smtp.sendmail(self.sender, self.receiver, self.msg.as_string())
        except Exception as msg:
            print("邮件发送失败！%s" % msg)
        else:
            print('邮件发送成功')
        finally:
            smtp.quit()
if __name__ == '__main__':
    ce = SendEmail()
    path1 = r"F:\code\vip20_framework\testReport\2022-05-29 115756report.html"
    ce.send(path1)


